package ubc;

import java.io.*;
import java.nio.ByteBuffer;
import java.util.ArrayList;

/**
 *
 * @author Samer Al-Kiswany
 */
public class CrawlResult implements Serializable{

    // Types of crawlStage
    public static int NOT_CRAWLED = 0;
    public static int SENT_PEERS_REQUEUST = 1;
    public static int GOT_PEERS_RESPONSE = 2;
    public static int ACKED_PEERS_RESPONSE = 3;
    public static int SENT_FILES_REQUEUST = 4;
    public static int CRAWLED_FILES = 5;

    // Types of Status
    public static int DISCOVERED = 0;
    public static int TIMEOUT_EXCEPTION = 1;
    public static int CONNECT_EXCEPTION = 2;
    public static int SEND_EXCEPTION = 3;
    public static int IO_EXCEPTION = 4;
    public static int CRAWLED = 5;

    private String ultrapeers;
    private String leaves;
    private int numOfFiles;
    private ArrayList<String> filesList;
    private ArrayList<Integer> filesListSize;
    private String Agent;
    private String host;
    private int port;
    private int status;

    private int crawlStage = 0;
    private long startTime = 0;

    public ByteBuffer buf;
    
    /** Creates a new instance of CrawlResults */
    public CrawlResult() {
        ultrapeers=new String();
        leaves=new String();
        numOfFiles=0;
        filesList=new ArrayList<String>();
        filesListSize = new ArrayList<Integer>();
        Agent=new String();
        host=new String();
        port=0;
        status = CrawlResult.DISCOVERED;
        startTime = System.currentTimeMillis();
        buf = ByteBuffer.allocate(100000);
    }

    public CrawlResult(String hostport) {
        ultrapeers=new String();
        leaves=new String();
        numOfFiles=0;
        filesList=new ArrayList<String>();
        filesListSize = new ArrayList<Integer>();
        Agent=new String();
        host = hostport.split(":")[0];
        port = Integer.parseInt(hostport.split(":")[1]);
        status = CrawlResult.DISCOVERED;
        startTime = System.currentTimeMillis();
        buf = ByteBuffer.allocate(100000);
    }

    public void setStage(int stage) {
        crawlStage = stage;
    }

    public int getStage() {
        return crawlStage;
    }

    public long getStartTime() {
        return startTime;
    }

    public void setHost(String h){
    	host = h;
    }
    
    public String getHost() {
    	return host;
    }
    
    public String getHostPort(){
    	return host + ":" + port;
    }
    
    public void setPort(int p) {
    	port = p;
    }
    
    public int getPort() {
    	return port;
    }
    
    public void setUltrapeers(String upeers){
        ultrapeers=upeers;
    }
    public String getUltrapeers(){
        return ultrapeers;
    }
    
    public void setLeaves(String lvs){
        leaves=lvs;
    }
    public String getLeaves(){
        return leaves;
    }
    
    public void addNumOfFiles(int num){
        numOfFiles+=num;
    }
    public int getNumOfFiles(){
        return numOfFiles;
    }
    
    public void addFileName(String fname){
        filesList.add(fname);
    }
    public void addFileSize(int fsize){
        filesListSize.add(fsize);
    }

    public void clearFilesList() {
        filesList.clear();
        filesListSize.clear();
    }

    public ArrayList<String> getFilesList(){
        return filesList;
    }
    public ArrayList<Integer> getFilesListSize(){
        return filesListSize;
    }
    
    public ArrayList<String> getFilesListExt() {
    	ArrayList<String> extensions = new ArrayList<String>();
		//loop through every filename and find if they have an extension 
		for (int i=0;i<filesList.size();i++){
			//check that the file has an extension
			if (filesList.get(i).lastIndexOf('.')!= -1)
				extensions.add(filesList.get(i).substring(filesList.get(i).lastIndexOf('.')));
				
		}
		return extensions;
	}
    
    public void setAgent(String agnt){
        Agent=agnt;
    }
    public String getAgent(){
        return Agent;
    }
    public void setStatus(int s) {
    	status = s;
    }
    public int getStatus() {
    	return status;
    }

    public String getStatusString() {
        if ( status == CrawlResult.DISCOVERED ) return "Discovered";
        if ( status == CrawlResult.TIMEOUT_EXCEPTION ) return "Timeout Exception";
        if ( status == CrawlResult.CONNECT_EXCEPTION ) return "Connect Exception";
        if ( status == CrawlResult.IO_EXCEPTION ) return "IO Exception";
        if ( status == CrawlResult.SEND_EXCEPTION ) return "Send Exception";
        if ( status == CrawlResult.CRAWLED ) return "Crawled";
        return null;
    }

    public void print(){
    	System.out.println("Host : " + host + ":" + port);
    	System.out.println("Status : " + status);
        System.out.println("Running Aggent : "+getAgent());
        System.out.println("UltraPeers : "+getUltrapeers());
        System.out.println("Leaves : "+getLeaves());
        System.out.println("Number of files: "+getNumOfFiles());
        System.out.println("List of files :");
        for(int i=0; i<filesList.size(); i++) {
        	System.out.println(filesList.get(i) + " @ " + filesListSize.get(i));
        }
    }
}
